<template>
    <page-decorator>
        <div class="row">
            <div class="col-lg-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>学生列表 </h5>
                        <div class="ibox-tools">
                            <!--<button class="btn btn-primary btn-xs" v-on:click="showEditDialog(0)">-->
                                <!--添加学生-->
                            <!--</button>-->
                        </div>
                    </div>
                    <div class="ibox-content">
                        <el-list :stateManager="stateManager"></el-list>
                    </div>
                </div>
            </div>
        </div>

        <el-dialog :stateManager="stateManager"></el-dialog>
        <el-create :stateManager="stateManager"></el-create>
        <el-calendar :stateManager="stateManager"></el-calendar>
        <el-exam :stateManager="stateManager"></el-exam>
        <confirm-modal v-if="stateManager.deleteDialogVisible" text="你确定要删除此项内容吗？" title="删除确认"
                       v-on:cancel="stateManager.deleteDialogVisible= false"  v-on:ok="deleteModel"
                      ></confirm-modal>
    </page-decorator>
</template>

<script>

    import dialog from './_dialog.vue';
    import table from './_table.vue';
    import create from './_create.vue';
    import calendar from './_calendar.vue';
    import exams from './_exam.vue';
    export default {
        components: {
            'el-dialog': dialog,
            'el-list': table,
            'el-create':create,
            'el-calendar':calendar,
            'el-exam':exams,
        },
        data: function () {
            return {
//                departments: [{label: '综合部', value: 1}, {label: '技术部', value: 2}, {label: '行政部', value: 3}],
                stateManager: {
                    refreshList: true,
                    editDialogVisible: false,
                    editModelId: 0,
                    deleteDialogVisible: false,
                    deleteModelId: 0,
                    createDialogVisible: false,
                    CalendarDialogVisible: false,
                    CalendarModelId : 0,
                    ExamDialogVisible : false,
                    ExamModelId: 0,
                },
                model: this.$modelDataSource({
                    url: 'api/students',
                    dataKey: 'model',
                    attributes: {
                        id: 0,
                    }
                })
            };
        },
        created: function () {
            this.$store.commit('setTitle', '学生管理');
        },
        methods: {
            showEditDialog: function (id = 0) {
                if(id){
                    this.stateManager.editModelId = id;
                    this.stateManager.editDialogVisible = true;
                }else{
                    this.stateManager.createDialogVisible = true;
                }

            },
            deleteModel: function () {
                this.stateManager.deleteDialogVisible = false;
                let self = this;
                self.model.id = this.stateManager.deleteModelId;
                    self.model.destroy(function () {
                        self.model.reset();
                        self.stateManager.refreshList = true;
                        toastr.success('删除成功！');
                    }, function () {
                        if (self.model.errors.permissions_errors) {
                            toastr.error(self.model.errors.permissions_errors);
                        }
                });
            }
        },
    }
</script>